﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;

namespace OracleStart
{
    public partial class MainWindow : Form
    {
        ServiceController OracleXETNSListener;
        ServiceController OracleServiceXE;
        public MainWindow()
        {
            InitializeComponent();
            OracleXETNSListener = new ServiceController("OracleXETNSListener");
            OracleServiceXE = new ServiceController("OracleServiceXE");
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (OracleXETNSListener.Status != ServiceControllerStatus.Running)
                    OracleXETNSListener.Start();
                if (OracleServiceXE.Status != ServiceControllerStatus.Running)
                    OracleServiceXE.Start();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

      
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (OracleXETNSListener.Status != ServiceControllerStatus.Stopped)
                    OracleXETNSListener.Stop();
                if (OracleServiceXE.Status != ServiceControllerStatus.Stopped)
                    OracleServiceXE.Stop();
            }
            catch (Exception excp)
            {
                MessageBox.Show(excp.ToString());
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            OracleXETNSListener.Refresh();
            switch (OracleXETNSListener.Status)
            {
                case ServiceControllerStatus.Stopped: 
                    label3.ImageIndex = 2;
                    break;
                case ServiceControllerStatus.Running:
                    label3.ImageIndex = 0;
                    break;
            }
            OracleServiceXE.Refresh();
            switch (OracleServiceXE.Status)
            {
                case ServiceControllerStatus.Stopped:
                    label4.ImageIndex = 2;
                    break;
                case ServiceControllerStatus.Running:
                    label4.ImageIndex = 0;
                    break;
            }
        }

        private void Form1_ResizeBegin(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                IkonaWTrayu.Visible = true;
                IkonaWTrayu.ShowBalloonTip(500);
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                IkonaWTrayu.Visible = false;
            }
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                this.ShowInTaskbar = false;
                IkonaWTrayu.Visible = true;
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                IkonaWTrayu.Visible = false;
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void IkonaWTrayu_MouseUp(object sender, MouseEventArgs e)
        {
        }
    }
}
